package com.jn.mvvm2.http;

import com.jn.mvvm2.interceptor.FormParamsInterceptor;
import com.jn.mvvm2.interceptor.HeaderInterceptor;
import com.jn.mvvm2.interceptor.QueryParamsInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class GlobalRetrofitClient {
   private static final String BASE_URL = "https://api.lolimi.cn/";
   private static Retrofit retrofit;

   private static OkHttpClient getOkHttpClient() {
      return new OkHttpClient.Builder()
              .connectTimeout(10, TimeUnit.SECONDS)
              .readTimeout(10, TimeUnit.SECONDS)
              .writeTimeout(10, TimeUnit.SECONDS)
              // 添加全局请求头
              .addInterceptor(new HeaderInterceptor())
              // 添加全局Query参数
              .addInterceptor(new QueryParamsInterceptor())
              // 添加全局Form参数
              .addInterceptor(new FormParamsInterceptor())
              .build();
   }

   public static Retrofit getInstance() {
      if (retrofit == null) {
         retrofit = new Retrofit.Builder()
                 .baseUrl(BASE_URL)
                 .client(getOkHttpClient())
                 .addConverterFactory(GsonConverterFactory.create())
                 .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                 .build();
      }
      return retrofit;
   }
}
